<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串变颜色</title>
    <script>

        window.onload = function (){
            setInterval(change,1000);
        }

        function change(){
            var str = document.getElementById("str");
            var r = Math.floor(Math.random()*256);
            var g = Math.floor(Math.random()*256);
            var b = Math.floor(Math.random()*256);
            str.style.color = "rgb("+r+","+g+","+b+")";
        }
    </script>
</head>
<body>
    <div id="str" onmouseover="change()">abcdefg</div>
</body>
</html>